package com.chen.base.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

/**
 * @author ChenJinLong
 */
@Slf4j
@Component
@ConditionalOnProperty(value = "server.shutdown", havingValue = "graceful")
public class ServerShutdownListener implements ApplicationListener<ContextClosedEvent> {

    public static Boolean lock = true;

    public ServerShutdownListener(){
        log.info("init ServerShutdownListener.....");
    }

    @Value("${spring.lifecycle.timeout-per-shutdown-phase:30s}")
    public String timeout;

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        if (lock){
            lock = false;
            log.info("监听到关机事件(timeout - {}).....", timeout);
        }
    }
}
